package org.summerframework.component.security.oauth.wechat;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.summerframework.component.httpclient.HttpParam;
import org.summerframework.component.security.oauth.OAuthAppIdAndAppSecret;
import org.summerframework.component.security.oauth.OAuthResultCode;
import org.summerframework.component.security.oauth.exception.WeCatException;
import org.summerframework.component.security.oauth.util.HttpClientFinshUtil;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * WeChatOAuthSupper
 *
 * @author 石超
 * @date 2016/3/25
 */
public abstract class AbstractWechatOAuthSupper implements WechatOAuthInterceptor {
    //access_token
    private static final String accessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";
    //刷新access_token
    //private String refreshTokenUrl = "https://api.weixin.qq.com/sns/oauth2/refresh_token";
    //获取用户个人信息
    private static final String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo";
    /*@Resource
    private OAuthCache oAuthCache;*/

    @Override
    public WechatUserInfo getResultInfo(WechatAccessToken wechatAccessToken) {
        HttpParam httpParam = new HttpParam();
        httpParam.setMethod(RequestMethod.POST);
        httpParam.setUrl(accessTokenUrl);
        httpParam.addParam("appid", getOAuthInfo().getAppId());
        httpParam.addParam("secret", getOAuthInfo().getAppSecret());
        httpParam.addParam("code", wechatAccessToken.getCode());
        httpParam.addParam("grant_type", wechatAccessToken.getGrantType());

        return fillAccessIoken(httpParam);
    }

    @Override
    public WechatUserInfo getInfo(WechatUserInfo wechatUserInfo) {
        HttpParam httpParam = new HttpParam();
        httpParam.setMethod(RequestMethod.POST);
        httpParam.setUrl(userInfoUrl);
        httpParam.addParam("access_token", wechatUserInfo.getAccessToken());
        httpParam.addParam("openid", wechatUserInfo.getOpenid());
        if (wechatUserInfo.getLang() != null) {
            httpParam.addParam("lang", wechatUserInfo.getLang());
        }
        JSONObject json = HttpClientFinshUtil.finsh(httpParam, "utf8");
        //判断是否有错
        if (json.containsKey("errcode")) {
            Integer errcode = json.getInteger("errcode");
            String errmsg = json.getString("errmsg");
            throw new WeCatException(OAuthResultCode.ILLEGAL_ARGUMENT, errcode + ": " + errmsg);
        }
        //用户的信息
        String openid = json.getString("openid");
        String nickname = json.getString("nickname");
        Integer sex = json.getInteger("sex");
        String province = json.getString("province");
        String city = json.getString("city");
        String country = json.getString("country");
        String headimgurl = json.getString("headimgurl");
        String unionid = json.getString("unionid");
        JSONArray jsonArray = json.getJSONArray("privilege");
        List<String> list = JSONObject.parseArray(jsonArray.toJSONString(), String.class);
        //UserInfo userInfo = new UserInfo(openid,nickname,sex,province,city,country,headimgurl,unionid);
        wechatUserInfo.setOpenid(openid);
        wechatUserInfo.setCity(city);
        wechatUserInfo.setCountry(country);
        wechatUserInfo.setHeadimgurl(headimgurl);
        wechatUserInfo.setNickname(nickname);
        wechatUserInfo.setSex(sex);
        wechatUserInfo.setUnionid(unionid);
        wechatUserInfo.setProvince(province);
        wechatUserInfo.setPrivilege(list);
        return wechatUserInfo;
    }

    private WechatUserInfo fillAccessIoken(HttpParam httpParam) {
        JSONObject json = HttpClientFinshUtil.finsh(httpParam, "utf8");

        //判断是否有错
        if (json.containsKey("errcode")) {
            Integer errcode = json.getInteger("errcode");
            String errmsg = json.getString("errmsg");
            throw new WeCatException(OAuthResultCode.ILLEGAL_ARGUMENT, errcode + ": " + errmsg);
        }

        String accessToken = json.getString("access_token");
        String openid = json.getString("openid");
        String scope = json.getString("scope");

        WechatUserInfo wechatUserInfo = new WechatUserInfo();
        wechatUserInfo.setAccessToken(accessToken);
        wechatUserInfo.setOpenid(openid);
        wechatUserInfo.setScope(scope);
        wechatUserInfo.setAppId(getOAuthInfo().getAppId());

        return wechatUserInfo;
    }

    @Override
    public WechatAccessToken getAccessToken(String code) {
        return new WechatAccessToken(code);
    }

    public abstract OAuthAppIdAndAppSecret getOAuthInfo();
}
